package com.zs.leymovie.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.adapter.YY_PtrAdapter;
import com.zs.leymovie.bean.AppBean;
import com.zs.leymovie.bean.BrandDataEntity;
import com.zs.leymovie.bean.CinemaDataEntity;
import com.zs.leymovie.bean.DataEntity;
import com.zs.leymovie.bean.DistrictDataEntity;
import com.zs.leymovie.bean.SpecialDataEntity;
import com.zs.leymovie.constants.UrlConstants;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.fragment_theatre)
public class SelectActivity extends AppCompatActivity implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener {
    @ViewInject(R.id.yy_ptrlistview)
    private PullToRefreshListView ptrListView;
    @ViewInject(R.id.yy_left_location)
    private TextView leftloc_btn;
    @ViewInject(R.id.yy_right_location)
    private ImageView img_loc;
    private YY_PtrAdapter ptrAdapter;
    private List<CinemaDataEntity> lists = new ArrayList<>();
    private List<String> distriList = new ArrayList<>();
    private List<String> brandList = new ArrayList<>();
    private List<String> specList = new ArrayList<>();
    private HttpUtils httpUtils;
    private HttpHandler handler;
    private PopupWindow pop;
    @ViewInject(R.id.yy_allarea)
    private TextView allarea;
    @ViewInject(R.id.yy_allpp)
    private TextView allpp;
    @ViewInject(R.id.yy_ts)
    private TextView ts;
    private ListView listView;
    private List<String> popList = new ArrayList<>();
    private ArrayAdapter adapter;
    private String city_id = "159";   //默认显示 深圳
    private String city_name;
    private String movie_id = "";
    private String movie_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        Intent intent = getIntent();
        city_id = intent.getStringExtra("city_id");
        movie_id = intent.getStringExtra("movie_id");
        movie_name = intent.getStringExtra("movie_name");
        leftloc_btn.setText(movie_name);
        initView();
        initData(city_id, movie_id);
    }

    private void initData(String cityid, String movie_id) {
        handler = httpUtils.send(HttpRequest.HttpMethod.GET, UrlConstants.getYY_URL(cityid, movie_id), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                ptrListView.onRefreshComplete();
                String result = responseInfo.result;
                AppBean app = JSONObject.parseObject(result, AppBean.class);
                if (app.getStatus() == 1) {   //成功
                    DataEntity data = app.getData();
                    if (data != null) {           //获取当前影院信息
                        List<CinemaDataEntity> tempcine = data.getCinema_data();
                        if (tempcine != null && tempcine.size() > 0) {
                            if (lists != null) {
                                lists.clear();
                            }
                            lists.addAll(tempcine);
                            ptrAdapter.notifyDataSetChanged();
                        }

                        List<DistrictDataEntity> tempdis = data.getDistrict_data();  //区域
                        if (tempdis != null && tempdis.size() > 0) {
                            if (distriList != null) {
                                distriList.clear();     //更新前清除
                            }
                            for (int i = 0; i < tempdis.size(); i++) {
                                distriList.add(tempdis.get(i).getName());
                            }
                        }

                        List<BrandDataEntity> tempbra = data.getBrand_data();     //品牌
                        if (tempbra != null && tempbra.size() > 0) {
                            if (brandList != null) {
                                brandList.clear();
                            }
                            for (int i = 0; i < tempbra.size(); i++) {
                                brandList.add(tempbra.get(i).getName());
                            }
                        }

                        List<SpecialDataEntity> tempspe = data.getSpecial_data();  //特色
                        if (tempspe != null && tempspe.size() > 0) {
                            if (specList != null) {
                                specList.clear();
                            }
                            for (int i = 0; i < tempspe.size(); i++) {
                                specList.add(tempspe.get(i).getName());
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                ptrListView.onRefreshComplete();
                Toast.makeText(SelectActivity.this, "数据加载失败，请稍候再试", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        httpUtils = new HttpUtils();
        ptrListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        ptrListView.setOnRefreshListener(this);
        ptrAdapter = new YY_PtrAdapter(lists, this);
        ptrListView.setAdapter(ptrAdapter);
        ptrListView.setOnItemClickListener(this);
        ptrListView.setRefreshing();

        final View popView = LayoutInflater.from(this).inflate(R.layout.pop_listview, null);
        listView = (ListView) popView.findViewById(R.id.pop_list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {    //poplist的点击事件
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view.findViewById(R.id.pop_item_text);
                Toast.makeText(SelectActivity.this, tv.getText(), Toast.LENGTH_LONG).show();
                pop.dismiss();
            }
        });

        adapter = new ArrayAdapter(this, R.layout.yy_headpop_item, popList);    //设置pop适配器
        listView.setAdapter(adapter);
        pop = new PopupWindow(popView);
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //pop.setFocusable(true);
        pop.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); //设置背景后点击外部不能让它消失，跟下面的冲突activity
    }

    public void clear() {
        if (popList != null) {     //清除之前的数据
            popList.clear();
        }
        if (!pop.isShowing() && lists.size() > 0) {
            pop.showAsDropDown(allpp);   //显示
        } else {
            pop.dismiss();               //消失
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        initData(city_id, movie_id);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
    }

    /**
     * ptrlistview的点击事件
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (pop != null) {
            pop.dismiss();
        }
        //启动另一个activity
        Intent intent = new Intent(this, YyItemActivity.class);
        CinemaDataEntity entity = lists.get(position - 1);
        intent.putExtra("cimeadata", entity);  //电影院的ID
        intent.putExtra("cityid", city_id);     //城市ID
        startActivity(intent);
    }
}
